﻿app.factory("itemfactory", function () {
    return [];
});


app.controller("ngMainController", function ($scope, $http, $window, $q, $location, itemfactory, $route, $interval) {
    $scope.pageClassAni = "page-ngmain";
    $scope.items = itemfactory;
    $scope.tickcount = 0;
    var pageInitialized = false;
    var slowCheck = false;

    $scope.userBids = 0;
    $scope.userWallet = 0;

    var getUserInfo = function () {
        if ($scope.username) {

            $http.get("/api/user/detail/").success(function (data) {
                $scope.userBids = data.BidCount;
                $scope.userWallet = data.Wallet;
            }).error(function (data) {
                console.log(data);
            });
        }

    }
    getUserInfo();

    $http.get("/api/item/index/").success(function (data) {
        //console.log(data);
        itemfactory = [];
        $scope.items = data;
        $scope.tickcount = 0;
        pageInitialized = true;
    }).error(function (data) {
        console.log(JSON.stringify(data))
    });

    var getDetailForNewItem = function (id) {
        $http.get("/api/item/detail/" + id).success(function (data) {
            data.wasUpdated = true;
            $scope.items.push(data);
        }).error(function (data) {
            console.log(data);
        });
    };

    $scope.getUpdates = function () {
        $http.get("/api/item/quick/").success(function (data) {
            
            for (var i in data) {
                var freshItem = data[i];
                for(var j in $scope.items)
                {
                    var item = $scope.items[j];
                    if (freshItem.Id != item.Id) {
                        continue;
                    }
                    item.Cost = freshItem.Cost;
                    item.UserName = freshItem.UserName;
                    item.msRemaining = freshItem.msRemaining;
                    item.wasUpdated = true;
                    item.keepPolling = true;
                    freshItem.matched = true;
                }
                if (!freshItem.matched) {
                    getDetailForNewItem(freshItem.Id);
                }
            }
            $scope.tickcount = 0;

            for (var i in $scope.items) {
                var item = $scope.items[i];
                if (item.wasUpdated == false) {
                    item.msRemaining = 0;
                    item.isFinished = true;
                    if (item.keepPolling) {
                        getFinalUpdate(item);
                    }
                }
                item.wasUpdated = false;
            }
        }).error(function (data) {
            console.log(JSON.stringify(data))
        });
    };

    var getFinalUpdate = function (item) {
        $http.get("/api/item/quick/" + item.Id).success(function (data) {
            //console.log(data)
            item.UserName = data.UserName;
            item.Cost = data.Cost;
            item.keepPolling = false;
            console.log(item);
        }).error(function (data) {
            console.log(data);
        });
    }

    $scope.addTime = function (id) {
        document.getElementsByClassName(id).inner
    }

    $scope.bidSubmit = function (item) {
        $http.post("/api/Item/bid/" + item.Id).success(function (data) {

            item.Cost = data.Cost;
            item.msRemaining = data.msRemaining;
            item.UserName = data.UserName;
            $scope.userBids--;
            getUserInfo();

        }).error(function (data) {
            console.log(JSON.stringify(data));
        });
    };

    $scope.ItemDetail = function (id) {
        $location.path("/ItemDetail/" + id);
    }

    var clockTick = $interval(function () {
        //console.log("tick...");
        $scope.tickcount += 1000;
        if ($scope.username && !slowCheck) {
            getUserInfo();
            slowCheck = true;
        }
    }, 1000);

    var refresh = $interval(function () {
        //console.log("refresh");
        if (pageInitialized) {
            $scope.getUpdates();
        }
    }, 5000);

    $scope.$on('$destroy', function () { $interval.cancel(clockTick); $interval.cancel(refresh); });

    $scope.selectgenre = function (i) {
        $scope.genre = "";
        if (i == 1) {
            $scope.genre = "Smartwatches";
        } else if (i == 2) {
            $scope.genre = "Smartphones";
        } else if (i == 3) {
            $scope.genre = "Computers/Laptops";
        } else if (i == 4) {
            $scope.genre = "Artificial Intelligence/Robots";
        } else if (i == 5) {
            $scope.genre = "Furistic Technology";
        } else if (i == 6) {
            $scope.genre = "Flying Things";
        }
    }

});
app.filter('msToTs', function () {
    return function (millseconds) {
        var seconds = Math.floor(millseconds / 1000);
        var hours = Math.floor(seconds / 3600);
        seconds %= 3600;
        var minutes = Math.floor(seconds / 60);
        var sec = seconds % 60;
        var timeString = '';
        if (hours > 0) timeString += (hours > 1) ? (hours + " hours ") : (hours + " hour ");
        if (minutes >= 0) timeString += (minutes > 1) ? (minutes + " minutes ") : (minutes + " minute ");
        if (sec >= 0) timeString += (sec > 1) ? (sec + " seconds ") : (sec + " second ");
        return timeString;
    }
});